package com.zzq.service.impl;

import com.zzq.mapper.MessageMapper;
import com.zzq.pojo.Message;
import com.zzq.pojo.vo.HotTenderInfo;
import com.zzq.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class MessageServiceImpl implements MessageService {

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<Message> getMessageList() {
        return messageMapper.getMessageList();
    }

    @Override
    public void deleteMessage(int id) {
        messageMapper.deleteMessage(id);
    }

    @Override
    public void addMessage(Message message) {
        messageMapper.addMessage(message);
    }

    @Override
    public List<Message> getNewMessageList(int page, int size) {

        String cacheKey = "newMessage_page_" + page + "_size_" + size;

        // 尝试从缓存获取
        List<Message> newMessage = (List<Message>) redisTemplate.opsForValue().get(cacheKey);

        if (newMessage == null) {
            // 缓存未命中，从数据库获取
            newMessage = messageMapper.getNewMessageList(page,size);

            // 放入缓存，设置5分钟过期
            redisTemplate.opsForValue().set(cacheKey, newMessage, 5, TimeUnit.MINUTES);
        }

        return newMessage;
    }

    @Override
    public Message getNoticeById(int id) {
        return messageMapper.getNoticeById(id);
    }
}
